package com.yanqu.road.dao.impl.activity.daomu.pvp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.daomu.pvp.DaoMuPvpData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class DaoMuPvpDataDaoImpl extends TempDataObjectDao<DaoMuPvpData> {

    public static String getDaoMuPvpDataSql = "select * from `t_c_activity_daomu_pvp_data` where `activityId`=?";

    public DaoMuPvpData getDaoMuPvpData(int activityId) {
        DaoMuPvpData daoMuPvpData = null;
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDaoMuPvpDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                daoMuPvpData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return daoMuPvpData;
    }

    public static String insertDaoMuPvpDataSql = "insert into `t_c_activity_daomu_pvp_data`(activityId,round,lockState,floorCntMap,disuse,settle) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(DaoMuPvpData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getFloorCntMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDisuse()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSettle()));
        if (getDb().executeNoneQuery(insertDaoMuPvpDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDaoMuPvpDataSql = "update `t_c_activity_daomu_pvp_data` set round=?,lockState=?,floorCntMap=?,disuse=?,settle=? where activityId=? ";

    @Override
    protected boolean updateData(DaoMuPvpData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getFloorCntMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDisuse()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSettle()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateDaoMuPvpDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DaoMuPvpData parseData(ResultSet rs) throws SQLException {
        DaoMuPvpData data = new DaoMuPvpData();
        data.setActivityId(rs.getInt("activityId"));
        data.setRound(rs.getInt("round"));
        data.setLockState(rs.getBoolean("lockState"));
        data.setFloorCntMap(JSON.parseObject(rs.getString("floorCntMap"), new TypeReference<Map<Integer, Integer>>() {
        }));
        data.setDisuse(rs.getBoolean("disuse"));
        data.setSettle(rs.getBoolean("settle"));
        return data;
    }
}
